<?php

Configure::load("antispam");

class SpammersBlacklistEntry extends AppModel {
    var $primaryKey = 'ip';
    var $useTable = "spammers_blacklist";

    public function updateBlacklist() {
        if (Configure::read("Antispam.useIpBlacklist")) {
            if (Configure::read("Antispam.updateIpBlacklist")) {
                $this->deleteAll(array());

                App::import('Core', 'HttpSocket');
                $HttpSocket = new HttpSocket();
                $HttpSocket->get(Configure::read("Antispam.ipBlacklistUrl"));
                $data = array();
                foreach (explode("\n", str_replace("\r", "", $HttpSocket->response['body'])) as $line) {
                    if (!startsWith($line, "#") && $line != "") { //skip comments
                        $data[]['ip'] = $line;
                    }
                }
                //debug($data);
                if ($this->saveAll($data)) {
                    return("Blacklist saved.");
                }
                else
                    return("Cannot save blacklist!");
            } else {
                return("Ip blacklist update is disabled.");
            }
        } else {
            return("Ip blacklist is disabled.");
        }
    }

}

?>
